package cn.com.generaldata.os.service.indicator_evaluation.industry_structure_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 第二产业经济弹性系数评估指标计算类.
 */
public class SecondEconomicElasticCoefficientCalculateService extends IndicatorEvaluationCalculateService {

	private String FirstName = ProvinceIndicatorConstant.FIRST_INDICATOR_NAME;
	private String SecondName = ProvinceIndicatorConstant.SECOND_INDICATOR_NAME;
	private String ThirdName = ProvinceIndicatorConstant.THIRD_INDICATOR_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		// 同比年份
		int fixBaseYear = indicatorAccountVO.getYear() - 1;
		Double firstBase = geFirstIndicatorVal(indicatorAccountVO, fixBaseYear, FirstName);
		Double secontBase = geFirstIndicatorVal(indicatorAccountVO, fixBaseYear, SecondName);
		Double thirdBase = geFirstIndicatorVal(indicatorAccountVO, fixBaseYear, ThirdName);
		// 当期第一产业
		Double firstCurrent = geFirstIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear(), FirstName);
		// 当期第二产业
		Double secondCurrent = geFirstIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear(), SecondName);
		// 当期第三产业
		Double thirdCurrent = geFirstIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear(), ThirdName);
		Double sumCurrent = firstCurrent + secondCurrent + thirdCurrent;
		Double sumBase = firstBase + secontBase + thirdBase;
		// if (secontBase.equals(Double.valueOf(0)) || sumCurrent.equals(Double.valueOf(0))
		// || sumBase.equals(Double.valueOf(0))) {
		// return Double.valueOf(0);
		// }
		Validate.isTrue(!(Double.valueOf(0).equals(secontBase)));
		Validate.isTrue(!(Double.valueOf(0).equals(sumCurrent)));
		Validate.isTrue(!(Double.valueOf(0).equals(sumBase)));
		return (secondCurrent / secontBase) / (sumCurrent / sumBase);

	}

	private Double geFirstIndicatorVal(IndicatorAccountVO indicatorAccountVO, int year, String name) {
		Double result = getInputOrAccountingIndicatorValue(indicatorAccountVO, year, name);
		return result == null ? 0d : result;
	}

}
